import java.util.ArrayList;
import java.util.List;

 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

public class Solution {
    List<Integer> list = new ArrayList<>();
    public boolean isValidBST(TreeNode root) {
        isLegal(root);
        int num1 = list.remove(0);
        for (int i = 0; i < list.size() - 2 ; i++) {
            int num2 = list.remove(0);
            if (num1 > num2) {
                return false;
            } else {
                num1 = num2;
            }
        }
        return true;

    }
    public void isLegal(TreeNode root) {
        if (root == null) {
            return ;
        }
        isLegal(root.left);
        list.add(root.val);
        isLegal(root.right);
    }
}